/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screens;

import GamePlay.CanvasGame;
import Objects.Bariel;
import Objects.Bottle;
import Objects.Player;
import Resources.GameResource;
import Resources.InputKey;
import Resources.Resource;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author ThanhTrung
 */
public class Game extends IScreen {

    public Game(CanvasGame canvas) {
        super(canvas);
        init();
    }

    public void init() {
        try {

            b1 = new Bottle();
            b2 = new Bottle();
            b3 = new Bottle();
            b4 = new Bottle();
//            b5 = new Bottle();
//            b6 = new Bottle();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        bariel = new Bariel(canvas);
        imgs = new Image[GameResource.img.length];
        aPlayer = new Player(canvas);
        load_screen();
    }

    public void load_screen() {
        imgs = Resource.load(GameResource.img);
    }

    public void un_load_screen() {
        Resource.unload(imgs);
    }

    public void keyEvent(int keycode, int event) {
        if (event == InputKey.KEY_EVENT_DOWN) {
            switch (keycode) {
                case InputKey.KEY_LEFT_SOFTKEY:
                    if (aPlayer.getLife() <= 0) {
                        aPlayer.setLife(Player.MAX_LIFE);
                        canvas.setScreen(new Game(canvas));
                        Runtime.getRuntime().gc();
                    }
                    break;
                case InputKey.KEY_RIGHT_SOFTKEY:
//                    if (isGameOver) {
//                        canvas.setScreen(new Menu(canvas));
//                        Runtime.getRuntime().gc();
//                    } else {
                    canvas.setScreen(new Menu(canvas));
                    Runtime.getRuntime().gc();
//                    }
                    break;
                case InputKey.KEY_RIGHT:
                    right();
                    break;
                case InputKey.KEY_LEFT:
                    left();
                    break;
                case InputKey.KEY_NUM4:
                    left();
                    break;
                case InputKey.KEY_NUM6:
                    right();
                    break;
            }

        }
    }

    public void left() {

        bariel.setScreen_x(bariel.getScreen_x() - PX_PER_STEP);
        canvas.repaint();
    }

    public void right() {
        bariel.setScreen_x(bariel.getScreen_x() + PX_PER_STEP);
        canvas.repaint();
    }

    public void update() {
        try {
            if (b1.checkCollided(bariel)) {
                aPlayer.setScore(aPlayer.getScore() + SCORE_PER_BOTTLE);
            }
            if (!b1.checkFail()) {
                aPlayer.setLife(aPlayer.getLife() - 1);
            }
            if (b2.checkCollided(bariel)) {
                aPlayer.setScore(aPlayer.getScore() + SCORE_PER_BOTTLE);
            }
            if (!b2.checkFail()) {
                aPlayer.setLife(aPlayer.getLife() - 1);
            }
            if (b3.checkCollided(bariel)) {
                aPlayer.setScore(aPlayer.getScore() + SCORE_PER_BOTTLE);
            }
            if (!b3.checkFail()) {
                aPlayer.setLife(aPlayer.getLife() - 1);
            }
            if (b4.checkCollided(bariel)) {
                aPlayer.setScore(aPlayer.getScore() + SCORE_PER_BOTTLE);
            }
            if (!b4.checkFail()) {
                aPlayer.setLife(aPlayer.getLife() - 1);
            }
//            if (b5.checkCollided(bariel)) {
//                aPlayer.setScore(aPlayer.getScore() + SCORE_PER_BOTTLE);
//            }
//            if (!b5.checkFail()) {
//                aPlayer.setLife(aPlayer.getLife() - 1);
//            }
//            if (b6.checkCollided(bariel)) {
//                aPlayer.setScore(aPlayer.getScore() + SCORE_PER_BOTTLE);
//            }
//            if (!b6.checkFail()) {
//                aPlayer.setLife(aPlayer.getLife() - 1);
//            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public void paint(Graphics g) {
        Runtime.getRuntime().gc();
        g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
        g.setColor(255, 0, 0);
        g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD | Font.STYLE_ITALIC, Font.SIZE_LARGE));
        //Game Over
        if (aPlayer.getLife() <= 0) {
            if (aPlayer.getScore() < 2000) {
                g.drawImage(imgs[GameResource.IMG_GOVER1], 0, 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() < 4000 && aPlayer.getScore() >= 2000) {
                g.drawImage(imgs[GameResource.IMG_GOVER2], 0, 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() < 6000 && aPlayer.getScore() >= 4000) {
                g.drawImage(imgs[GameResource.IMG_GOVER3], 0, 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() < 8000 && aPlayer.getScore() >= 6000) {
                g.drawImage(imgs[GameResource.IMG_GOVER4], 0, 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() < 10000 && aPlayer.getScore() >= 8000) {
                g.drawImage(imgs[GameResource.IMG_GOVER5], 0, 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() >= 10000) {
                g.drawImage(imgs[GameResource.IMG_GOVER6], 0, 0, Graphics.TOP | Graphics.LEFT);
            }
        } else {
            //Background
            g.drawImage(imgs[GameResource.IMG_BACKGROUND], 0, 0, Graphics.TOP | Graphics.LEFT);
            //Girl
            if (aPlayer.getScore() < 2000) {
                g.drawImage(imgs[GameResource.IMG_GIRL], canvas.getWidth() - imgs[GameResource.IMG_GIRL].getWidth(), 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() >= 2000 && aPlayer.getScore() < 4000) {
                g.drawImage(imgs[GameResource.IMG_GIRL1], canvas.getWidth() - imgs[GameResource.IMG_GIRL1].getWidth(), 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() >= 4000 && aPlayer.getScore() < 6000) {
                g.drawImage(imgs[GameResource.IMG_GIRL2], canvas.getWidth() - imgs[GameResource.IMG_GIRL2].getWidth(), 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() >= 6000 && aPlayer.getScore() < 8000) {
                g.drawImage(imgs[GameResource.IMG_GIRL3], canvas.getWidth() - imgs[GameResource.IMG_GIRL3].getWidth(), 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() >= 8000 && aPlayer.getScore() < 10000) {
                g.drawImage(imgs[GameResource.IMG_GIRL4], canvas.getWidth() - imgs[GameResource.IMG_GIRL4].getWidth(), 0, Graphics.TOP | Graphics.LEFT);
            } else if (aPlayer.getScore() >= 10000) {
                g.drawImage(imgs[GameResource.IMG_GIRL5], canvas.getWidth() - imgs[GameResource.IMG_GIRL5].getWidth(), 0, Graphics.TOP | Graphics.LEFT);
            }

            //Bottle & Bariel
            b1.paint(g);
            b2.paint(g);
            b3.paint(g);
            b4.paint(g);
//            b5.paint(g);
//            b6.paint(g);

            bariel.paint(g);

            //Border
            g.drawImage(imgs[GameResource.IMG_BG_PLAY], 0, 0, Graphics.TOP | Graphics.LEFT);

            //Life & Score
            for (int i = 0; i < aPlayer.getLife(); i++) {
                g.drawImage(imgs[GameResource.IMG_LIFE], 10 + 15 * i, 10, Graphics.TOP | Graphics.LEFT);
            }
            g.drawString(String.valueOf(aPlayer.getScore()), 200 - 4 * (String.valueOf(aPlayer.getScore()).length()), 10, Graphics.TOP | Graphics.LEFT);

            //Update
            update();
        }
    }
    Bottle b1;
    Bottle b2;
    Bottle b3;
    Bottle b4;
//    Bottle b5;
//    Bottle b6;
    private Image[] imgs;
    private Bottle bottle;
    private Bariel bariel;
    private Bottle[] bottles;
    private static final int NUM_OF_BOTTLE = 6;
    private static final int SCORE_PER_BOTTLE = 100;
    private int level = 1;
    private int score = 0;
    private boolean isGameOver = false;
    Player aPlayer = null;
    private static final int PX_PER_STEP = 6;
}
